﻿using System;
using System.Xml.Linq;
using DevDefined.OAuth.Consumer;
using DevDefined.OAuth.Framework;


namespace Lightray.Hattrick.ChppLib.OAuth
{
    public class DevDefinedOAuth : IOAuth
    {
        private const string REQUEST_TOKEN_URL = "https://chpp.hattrick.org/oauth/request_token.ashx";
        private const string USER_AUTHORIZE_URL = "https://chpp.hattrick.org/oauth/authorize.aspx";
        private const string ACCESS_TOKEN_URL = "https://chpp.hattrick.org/oauth/access_token.ashx";
        private const string AUTHENTICATE_URL = "https://chpp.hattrick.org/oauth/authenticate.aspx";
        private const string CHECK_TOKEN_URL = "https://chpp.hattrick.org/oauth/check_token.ashx";
        private const string INVALIDATE_TOKEN_URL = "https://chpp.hattrick.org/oauth/invalidate_token.ashx";
        private const string HTTP_GET = "GET";

        private OAuthSession _oauthSession = null;
        private IToken _requestToken = null;

        public IToken AccessToken 
        {
            get
            {
                return _oauthSession.AccessToken;
            }
            set
            {
                _oauthSession.AccessToken = value;
            }

        }

        public DevDefinedOAuth(string consumerKey, string consumerSecret)
        {
            initOAuthSession(consumerKey, consumerSecret);
        }

        public DevDefinedOAuth(string consumerKey, string consumerSecret, IToken accessToken)
            : this(consumerKey, consumerSecret)
        {
            AccessToken = accessToken;
        }

        public string GetAuthorizeUrl()
        {
            const string NO_CALLBACK = "oob";
            _requestToken = _oauthSession.GetRequestToken(HTTP_GET);
            return
                _oauthSession.GetUserAuthorizationUrlForToken(_requestToken, NO_CALLBACK);
        }

        public IToken ExchangeRequestTokenForAccessToken(string verificationCode)
        {
            if (_requestToken == null)
                throw new Exception("Request token has not yet been initialized");

            return
                _oauthSession.ExchangeRequestTokenForAccessToken(_requestToken, HTTP_GET, verificationCode);
        }

        public XDocument AccessProtectedResource(string url)
        {
            if (AccessToken == null)
                throw new Exception("Access token has not yet been initialized");

            return _oauthSession.Request().Get().ForUrl(url).ToDocument();
        }

        private void initOAuthSession(string consumerKey, string consumerSecret)
        {

            OAuthConsumerContext consumerContext =  new OAuthConsumerContext() 
            {
                ConsumerKey = consumerKey,
                ConsumerSecret = consumerSecret,
                SignatureMethod = SignatureMethod.HmacSha1,
            };

            _oauthSession = new OAuthSession(consumerContext, REQUEST_TOKEN_URL, USER_AUTHORIZE_URL, ACCESS_TOKEN_URL);
        }    
    }
}







